function refresh() {
	var index = $("#tabs").tabs("option", "selected");
	var url = "profile.jsp?tabIndex=" + index;
	window.location.href = url;
}
function creditCard() {
	var serial = Card.readSerial();//"2F177F3C";//
	if (serial == null){
		alert("请将你的员工卡放到读卡器上");
		return;
	}
	GlobalAjax.getUserByCardSerialNumber(serial, function(data){
		if (data != null && data.userId) {
			$("#userId").val(data.userId);
			$("#cardNumber").val(data.cardNumber);
			$("#userName").html(data.name + "(工号:" + data.workNumber + ")");//data.workNumber + " (" + data.name + ")"
		} else {
			alert("无效卡, 或卡已经被禁用, 请重试或与管理员联系");
		}
	});
}
function saveProfile(){
	var nickName = $("#user_nickname").val();
//	var gender = $("input:radio[@name=user_gender]").val();
	var gender = $("#user_gender").val();
	var phone = $("#user_phone").val();
	var birthday = $("#user_birthday").val();
	$("#submit_button").attr("disable", "true");
	
	GlobalAjax.updateProfile(nickName, gender, phone, birthday, function(data){
		$("#submit_button").attr("disable", "false");
		if (data == null){
			alert("保存成功");
			refresh();
		} else {
			alert(data);
		}
	});
}
function savePassword(){
	var oldPass = $("#oldPassword").val();
	var newPass = $("#newPassword").val();
	var repeat = $("#repeat").val();

	if (newPass == "") {
		alert("新输入新密码!");
		return;
	}
	if (newPass != repeat) {
		alert("重复密码与密码不一致!");
		return;
	}
	
	GlobalAjax.updatePassword(oldPass, newPass, function(data){
		$("#submit_button").attr("disable", "false");
		if (data == null){
			alert("保存成功");
			refresh();
		} else {
			alert(data);
		}
	});
}
function resetDialog(dialog) {
	$("#userId").val('');
	$("#cardNumber").val('');
	$("#userName").html('');
	$("#workNumber").val('');
	$("#password").val('');
	$("#amount").hide();
}
function startDuty() {
	if (!confirm("在非特殊情况下你应该通过与当前值班人交接, 而不是直接开始值班. \n 是否要开始新的值班? ")) {
		return;
	}
	DutyAjax.startDuty(function(data){
		if (data == null) {
			alert("开始值班");
			refresh();
		} else {
			alert(data);
		}
	});
}
function pauseDuty() {
	if (!confirm("在正常情况下你应该通过与当前值班人交接而结束值班. \n 确认要暂停值班? ")) {
		return;
	}
	DutyAjax.pauseDuty(function(data){
		if (data == null) {
			alert("暂停值班成功");
			refresh();
		} else {
			alert(data);
		}
	});
}

function resumeDuty() {
	DutyAjax.resumeDuty(function(data){
		if (data == null) {
//			alert("恢复值班成功");
			refresh();
		} else {
			alert(data);
		}
	});
}
function acceptDuty() {
	var dialog = $("#selectUser");
	resetDialog(dialog);
	dialog.dialog({
//		autoOpen: false,
		height: 300,
		width: 400,
		modal: true,
		buttons: {
			'确定': function() {
				var mode = $("#utabs").tabs("option", "selected");
				var userId, cardNumber, workNumber, password;
				if (mode == 1) {//刷卡
					userId = $("#userId").val();
					cardNumber = $("#cardNumber").val();
				} else if (mode == 0) {	//输用户名和密码
					workNumber = $("#workNumber").val();
					password = $("#password").val();
				} else {
					return;
				}
				var THIS = this;
				accept(userId, cardNumber, workNumber, password, THIS, true);
			},
			'取消': function() {
				$(this).dialog('close');
			}
		}
	});
}

function accept(userId, cardNumber, workNumber, password, dialog, reconfirm) {
	DutyAjax.acceptDuty(userId, cardNumber, workNumber, password, reconfirm, function(data){
		if (data == null)
			return;
		if (data.result == 1) {
			alert("接受值班成功");
			$(dialog).dialog('close');
			refresh();
			return;
		}
		if (data.result == 2) {
			alert(data.description);
			return;
		}
		if (data.result == 3) {
			if (confirm(data.description))
				accept(userId, cardNumber, workNumber, password, dialog, false);
		}
	});
}

function passDuty() {
	var dialog = $("#selectUser");
	resetDialog(dialog);
	dialog.dialog({
		height: 300,
		width: 400,
		modal: true,
		buttons: {
			'确定': function() {
				var mode = $("#utabs").tabs("option", "selected");
				var userId, cardNumber, workNumber, password;
				if (mode == 1) {//刷卡
					userId = $("#userId", dialog).val();
					cardNumber = $("#cardNumber", dialog).val();
				} else if (mode == 0) {	//输用户名和密码
					workNumber = $("#workNumber", dialog).val();
					password = $("#password", dialog).val();
				} else {
					return;
				}
				var THIS = this;
				pass(userId, cardNumber, workNumber, password, THIS, true);
			},
			'取消': function() {
				$(this).dialog('close');
			}
		}
	});
}

function pass(userId, cardNumber, workNumber, password, dialog, reconfirm) {
	DutyAjax.passDuty(userId, cardNumber, workNumber, password, reconfirm, function(data){
		if (data == null)
			return;
		if (data.result == 1) {
			alert("交接值班成功");
			$(dialog).dialog('close');
			refresh();
			return;
		}
		if (data.result == 2) {
			alert(data.description);
			return;
		}
		if (data.result == 3) {
			if (confirm(data.description))
				pass(userId, cardNumber, workNumber, password, dialog, false);
		}
	});
}
function reserveMoney() {
	var dialog = $("#reserveDialog");
	dialog.dialog({
		autoOpen: true,
		height: 170,
		width: 350,
		modal: true,
		buttons: {
			'确定': function() {
				var money = $("#reserve").val();
				if (money === '')
					return;
				DutyAjax.reserve(money, function(data){
					if (data != null) {
						alert(data);
						return;
					}
					refresh();
				});
			},
			'取消': function() {
				$(this).dialog('close');
			}
		}
	});
}
function withdraw() {
	var dialog = $("#selectUser");
	resetDialog(dialog);
	$("#amount").show();
	dialog.dialog({
		height: 340,
		width: 400,
		modal: true,
		buttons: {
			'确定': function() {
				var mode = $("#utabs").tabs("option", "selected");
				var userId, cardNumber, workNumber, password;
				var amount = $("#withdraw", dialog).val();
				if (mode == 1) {//刷卡
					userId = $("#userId").val();
					cardNumber = $("#cardNumber").val();
				} else if (mode == 0) {	//输用户名和密码
					workNumber = $("#workNumber").val();
					password = $("#password").val();
				} else {
					return;
				}
				var THIS = this;
				withdraw2(userId, cardNumber, workNumber, password, amount, THIS, true);
			},
			'取消': function() {
				$(this).dialog('close');
			}
		},
		close : function() {
			$("#amount").hide();
		}
	});
}

function withdraw2(userId, cardNumber, workNumber, password, amount, dialog, reconfirm) {
	DutyAjax.withdraw(userId, cardNumber, workNumber, password, reconfirm, amount, function(data){
		if (data == null)
			return;
		if (data.result == 1) {
			alert("财务提取成功");
			$(dialog).dialog('close');
			return;
		}
		if (data.result == 2) {
			alert(data.description);
			return;
		}
		if (reconfirm && data.result == 3) {
			if (confirm(data.description))
				withdraw2(userId, cardNumber, workNumber, password, amount, dialog, false);
		}
	});
}

$(function() {
//	$.datepicker.setDefaults($.datepicker.regional['zh-CN']);
	var birthday = $("#user_birthday").val();
	$("#user_birthday").datepicker().datepicker('option', {dateFormat:"yy-mm-dd"});
	$("#user_birthday").val(birthday);
	$("#submit_password").click(savePassword);
	$("#submit_profile").click(saveProfile);
	$("#startDuty").click(startDuty);
	$("#resumeDuty").click(resumeDuty);
	$("#pauseDuty").click(pauseDuty);
	$("#acceptDuty").click(acceptDuty);
	$("#passDuty").click(passDuty);
	$("#creditCard").click(creditCard);
	$("#withdraw").click(withdraw);
	$("#refresh").click(refresh);
	$("#reserveMoney").click(reserveMoney);
	$("input:text, select").width("200px");
	$("#tabs").tabs();
	$("#tabs").tabs({"selected": window.tabIndex});
	$("#utabs").tabs();
	$("#tabs>div").height("350px");
});
